;(function($) {
	$.widget('bpm.report', {
		options: {
			source: null,
			columns: null,
			maxRows: null
		},
		
		_create: function() {
			var self = this;
			
			self._bindEvents();
			self._loadDataTable();
		},
		
		_bindEvents: function() {
			var self = this;
			
			$('#btnFilter').click(function(e) {
				self.table.fnDraw();

				e.preventDefault();
			});
		},
		
		_loadDataTable: function() {
			var self = this;
			
			self.table = $(self.element).dataTable({
				"bProcessing" : true,
				"bServerSide" : true,
				"sAjaxSource": self.options.source,
				"sServerMethod" : "POST",
				"bAutoWidth": false,
				"bFilter": false,
				"bLengthChange" : false,
				"bInfo": true,
				"bPaginate": true,
		        "iDisplayLength" : self.options.maxRows,
				"oLanguage": { 
					"sEmptyTable": $('<div>').append($.tableEmptyMessage).html(),
					"sInfo":         "Mostrando de _START_ até _END_ de _TOTAL_ registros",
				    "sInfoEmpty":    "Mostrando de 0 até 0 de 0 registros",
				    "sInfoFiltered": "",
				    "sInfoPostFix":  "",
				    "sProcessing":   "Carregando...",
					"oPaginate": {
				        "sFirst":    "Primeiro",
				        "sPrevious": "Anterior",
				        "sNext":     "Próximo",
				        "sLast":     "Último"
				    }
				},
				aoColumns: self.options.columns,
				"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
					$(nRow).addClass('rowLink');
					$(nRow).attr('data-link', 'row');
				},
				"fnServerParams" : function(aoData) {
					$('#frmFilter select,input').each(function(index, elm) {
						switch (elm.type) {
							case 'checkbox':
							case 'radio':
								if (elm.checked) {
									aoData.push({"name" : elm.name, "value" : elm.value});
								}
								break;
							default:
								aoData.push({"name" : elm.name, "value" : elm.value});
								break;	
						}
					});
				}
			});
		}
	});
})(jQuery);